home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 2010 April / PCWorld0410.iso / pluginy Firefox / 53627 / 53627.xpi / chrome / content / mobilize / mobilize.js next >
Text File  |  2010-01-15  |  4KB  |  104 lines

  1. /*
  2.  * Mobilize Copyright (C) 2009 Attila Csipa
  3.  *
  4.  * Code relased under the GNU General Public Licence (GPL), version 2 or later
  5.  *
  6.  * http://www.gnu.org/licenses/gpl.html
  7.  *
  8.  * URI parser segment based on
  9.  * parseUri 1.2.2
  10.  * (c) Steven Levithan <stevenlevithan.com>
  11.  * MIT License
  12.  */
  13.  
  14. var Mobilize = {
  15.  
  16.     DEBUG_MODE : true,
  17.  
  18.     log : function(msg) {
  19.         if (!this.DEBUG_MODE)
  20.         {
  21.             return;
  22.         }
  23.         if (!this.consoleService)
  24.             this.consoleService = Components.classes["@mozilla.org/consoleservice;1"].getService(Components.interfaces.nsIConsoleService);
  25.         this.consoleService.logStringMessage('Mobilize: ' + msg);
  26.     },
  27.  
  28.     bleep : function(msg) {
  29.         alert(msg)
  30.     },
  31.    
  32.  
  33.     parseUri : function (str) {
  34.         var o   = {
  35.             strictMode: false,
  36.             key: ["source","protocol","authority","userInfo","user","password","host","port","relative","path","directory","file","query","anchor"],
  37.             q:   {
  38.                 name:   "queryKey",
  39.                 parser: /(?:^|&)([^&=]*)=?([^&]*)/g
  40.             },
  41.             parser: {
  42.                 strict: /^(?:([^:\/?#]+):)?(?:\/\/((?:(([^:@]*)(?::([^:@]*))?)?@)?([^:\/?#]*)(?::(\d*))?))?((((?:[^?#\/]*\/)*)([^?#]*))(?:\?([^#]*))?(?:#(.*))?)/,
  43.                 loose:  /^(?:(?![^:@]+:[^:@\/]*@)([^:\/?#.]+):)?(?:\/\/)?((?:(([^:@]*)(?::([^:@]*))?)?@)?([^:\/?#]*)(?::(\d*))?)(((\/(?:[^?#](?![^?#\/]*\.[^?#\/.]+(?:[?#]|$)))*\/?)?([^?#\/]*))(?:\?([^#]*))?(?:#(.*))?)/
  44.             }
  45.         }
  46.  
  47.         m   = o.parser[o.strictMode ? "strict" : "loose"].exec(str),
  48.         uri = {},
  49.         i   = 14;
  50.  
  51.         while (i--) uri[o.key[i]] = m[i] || "";
  52.  
  53.         uri[o.q.name] = {};
  54.         uri[o.key[12]].replace(o.q.parser, function ($0, $1, $2) {
  55.             if ($1) uri[o.q.name][$1] = $2;
  56.         });
  57.  
  58.         return uri;
  59.     },
  60.  
  61.  
  62.     switchuri : function(uri) { 
  63.         if (String(uri.substring(0,5))=="about") {
  64.             Mobilize.bleep("about can't be switched");
  65.             return
  66.         }
  67.         uriobj = Mobilize.parseUri(uri)
  68.         if (uriobj.host.search('www.google.com') >= 0 && uri.search ('dc=mgcgo') < 0) { // the Google treatment
  69.             uri = uriobj.protocol + "://m.google.com/go"
  70. //            if (uriobj.host == 'www.google.com')
  71. //                uri = uri.replace('\/\/www.google.com', '//mobile.google.com/go');
  72.         } else if (uriobj.host.search('amazon.com') >= 0 && uri.search('/gp/aw/') < 0) { // the Amazon treatment
  73.             uri = uri.replace('amazon.com', 'amazon.com/gp/aw/h.html?'); 
  74.         } else if (uriobj.host.search('www.facebook.com') >= 0 ) { 
  75.             uri = uri.replace('www.facebook', 'touch.facebook'); 
  76.         } else if (uriobj.host.search('slashdot.org') >= 0 ) { 
  77.             uri = uri.replace('slashdot.org', 'slashdot.org/palm/?'); 
  78.         } else if (uriobj.host.search('fandango.com') >= 0 ) { 
  79.             uri = uri.replace('www.fandango', 'mobile.fandango'); 
  80.         } else if (uriobj.host.search('wikipedia.org') >= 0 ) { 
  81.             uri = uri.replace('en.wikipedia.org', 'mobile.wikipedia.org'); 
  82.             uri = uri.replace('www.wikipedia.org', 'mobile.wikipedia.org'); 
  83.             uri = uri.replace(/\/wiki\//, '/transcode.php?go='); 
  84.  
  85. // http://m.technewstube.com/site/ars-technica/
  86.  
  87.  
  88.         } else if (uriobj.host.search('m.') == 0) {
  89.             uri = uri.replace('\/\/m.', '//www.');
  90.         } else {
  91.             if (uriobj.host.search('www.') > 0) {
  92.                 uri = uri.replace(/\/\/www./, '//m.');
  93.             } else {
  94.                 uri = uri.replace(/\/\//,'//m.');
  95.             }
  96.         }
  97.         window.content.location.replace(uri);
  98. //        alert(uri);        
  99. //        var xmlhttp = new XMLHttpRequest();
  100. //        xmlhttp.open("GET", uri, true);
  101. //        xmlhttp.send(null)
  102.     }
  103. }
  104.